package com.mk.Controller;


import com.mk.Entity.*;
import com.mk.Service.IBlogService;
import com.mk.Service.ICommentService;
import com.mk.Service.ITagService;
import com.mk.Service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.AbstractCollection;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    IBlogService blogService;

    @Autowired
    ITypeService typeService;

    @Autowired
    ITagService tagService;

    @Autowired
    ICommentService commentService;

    //到首页
    @GetMapping("/index")
    public ModelAndView toIndex(queryObject queryObject){
        ModelAndView mv=new ModelAndView();
        Integer count = blogService.queryCount(queryObject);
        List<Type> typeList = typeService.queryAll();
        List<Tag> tagList = tagService.queryAll();
        tagList = tagList.subList(0, 6);
        typeList=typeList.subList(0,5);
        mv.addObject("typeList",typeList);
        mv.addObject("tagList",tagList);
        mv.addObject("count",count);
        mv.setViewName("index");
        return mv;
    }

    //ajax请求博客
    @GetMapping("/listBlog")
    public String listBlog(queryObject queryObject , Model model){
        List<Blog> blogs = blogService.queryAll(queryObject);
        Integer count = blogService.queryCount(queryObject);
        PageResult<Blog> pageResult=new PageResult<>(queryObject.getCurrentPage(), count, queryObject.getPageSize(), blogs);
        JsonResult jsonResult=new JsonResult("查询成功",true,pageResult);
        model.addAttribute("jsonResult",jsonResult);
        return "index::blogs";
    }


    //到博客详情页面

    @GetMapping("/toBlog/{bid}")
    public ModelAndView toBlog(@PathVariable(value = "bid") Long bid){
        ModelAndView mv=new ModelAndView();
        Blog blog = blogService.queryOne(bid);
        mv.addObject("blog",blog);
//        System.out.println(bid);
//        System.out.println("blog--------"+blog);
        mv.setViewName("blog");
        return mv;
    }



    //动态评论

    @GetMapping("/getComments")
    public String getComments(Blog blog , Model model){
        List<Comment> comments = commentService.queryAll(blog);
        System.out.println(comments);
        System.out.println("//////////");
        System.out.println(comments.get(0).getChildComments());
        model.addAttribute("comments",comments);
        return "blog :: commentlist";
    }

    //插入评论

    @ResponseBody
    @GetMapping("/inOrupComment")
    public JsonResult inOrupComment(Comment comment){

        //操作时间
        comment.setCreateTime(new Date());
        if ((comment.getId()==null)){
            //新增
            commentService.insertComment(comment);
            JsonResult jsonResult=new JsonResult("新增成功",true,null);
            return jsonResult;
        }else {
            //更新
            commentService.updateComment(comment);
            JsonResult jsonResult=new JsonResult("更改成功",true,null);
            return jsonResult;
        }
    }


    //到分类页面

    @GetMapping("/type")
    public String toType(Model model){
        Integer typeCount = typeService.queryCount();
        List<Type> typeList = typeService.queryAll();
        model.addAttribute("typeList",typeList);
        model.addAttribute("typeCount",typeCount);
        return "type";
    }

    //到tag页面


    @GetMapping("/tag")
    public String toTag(Model model){
        Integer tagCount = tagService.queryCount();
        List<Tag> tagList = tagService.queryAll();
        model.addAttribute("tagCount",tagCount);
        model.addAttribute("tagList",tagList);
        return "tag";
    }

    //ajax请求博客,根据标签id
    @GetMapping("/listBlogByTagId")
    public String listBlogByTagId(queryObject queryObject , Model model){
//        System.out.println("////////////////////");
//        System.out.println(queryObject.getKeytagid());
        List<Blog> blogs = blogService.queryByTagId(queryObject);
        Integer count = blogService.queryCount(queryObject);
        PageResult<Blog> pageResult=new PageResult<>(queryObject.getCurrentPage(), count, queryObject.getPageSize(), blogs);
        JsonResult jsonResult=new JsonResult("查询成功",true,pageResult);
        model.addAttribute("jsonResult",jsonResult);
        return "index::blogs";
    }

    //到归档页

    @GetMapping("/archives")
    public String toArchives(Model model){
        return "archives";
    }


    @GetMapping("/listBlogByTime")
    public String listBlogByTime(queryObject queryObject,Model model){
        List<String> strings = blogService.queryAllTime(queryObject);
        List<String> strings1 = blogService.queryTime();
        Integer totalCount = strings1.size();


        System.out.println("///////////////");
        System.out.println("strings::::"+strings);
        System.out.println("///////////////////////////////////////");
        System.out.println("strings1::::"+strings1);
        System.out.println("///////////////");
        //strings放入jsonResult中用于分页
        PageResult<String> pageResult=new PageResult<>(queryObject.getCurrentPage(),totalCount, queryObject.getPageSize(), strings);
        JsonResult jsonResult=new JsonResult("查询成功",true,pageResult);
        if (strings!=null){
            List<List<Blog>> lists = blogService.queryBlogByTime(strings);
            model.addAttribute("lists",lists);
        }else {
            model.addAttribute("lists",null);
        }
        model.addAttribute("jsonResult",jsonResult);
        model.addAttribute("totalCount",totalCount);
        return "archives::blogs";
    }
    
    
    //到关于我页面


    @GetMapping("/aboutme")
    public String toAboutme(){
        return "aboutme";
    }
}
